package com.campus.client.view;

import com.campus.client.controller.AdminCourseController;
import com.campus.client.net.ClientConnector;
import com.campus.common.vo.Classroom;
import com.campus.common.vo.Course;
import com.campus.common.vo.Student;
import com.campus.common.vo.Teacher;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;

import java.io.IOException;
import java.util.List;

public class AdminCourseView {

    private Accordion accordion;
    private ScrollPane scrollPane;
    private AdminCourseController controller;

    /** 课程视图模式 */
    public enum CourseViewMode {
        ALL, QUERY, ADD, DELETE
    }

    private CourseViewMode mode;
    private String queryKeyword;

    public AdminCourseView() throws IOException {
        this(CourseViewMode.ALL);
    }

    public AdminCourseView(CourseViewMode mode) throws IOException {
        this.mode = mode;
        controller = new AdminCourseController(new ClientConnector("172.20.10.2", 8888));
        initScrollPane();
        loadCourses();
    }

    public AdminCourseView(CourseViewMode mode, String keyword) throws IOException {
        this.mode = mode;
        this.queryKeyword = keyword;
        controller = new AdminCourseController(new ClientConnector("172.20.10.2", 8888));
        initScrollPane();
        loadCourses();
    }

    public ScrollPane getView() {
        return scrollPane;
    }

    private VBox mainBox;

    /** 初始化 ScrollPane 和表头 */
    private void initScrollPane() {
        mainBox = new VBox(0);
        mainBox.setFillWidth(true);

        accordion = new Accordion();
        accordion.setMaxWidth(Double.MAX_VALUE);

        // 表头
        HBox header = new HBox();
        header.getStyleClass().add("academic-course-header");
        header.setAlignment(Pos.CENTER_LEFT);
        header.setPadding(new Insets(4, 10, 4, 10));
        header.setSpacing(5);
        header.setMaxWidth(Double.MAX_VALUE);

        Label lblCourseId = new Label("课程编号");
        lblCourseId.setPrefWidth(140);
        lblCourseId.setAlignment(Pos.CENTER_LEFT);

        Label lblCourseName = new Label("课程名称");
        HBox.setHgrow(lblCourseName, Priority.ALWAYS);
        lblCourseName.setMaxWidth(Double.MAX_VALUE);
        lblCourseName.setAlignment(Pos.CENTER_LEFT);

        Label lblCredit = new Label("学分");
        lblCredit.setPrefWidth(80);
        lblCredit.setAlignment(Pos.CENTER);

        // 列：开课学院（固定宽）
        Label lblDepartment = new Label("开课学院");
        lblDepartment.setPrefWidth(120);
        lblDepartment.setAlignment(Pos.CENTER);

        Label lblClassCount = new Label("教学班个数");
        lblClassCount.setPrefWidth(100);
        lblClassCount.setAlignment(Pos.CENTER_RIGHT);

        header.getChildren().addAll(lblCourseId, lblCourseName, lblCredit, lblDepartment, lblClassCount);
        mainBox.getChildren().addAll(header, accordion);

        scrollPane = new ScrollPane(mainBox);
        scrollPane.setFitToWidth(true);
        scrollPane.setFitToHeight(true);
        scrollPane.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
        scrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.AS_NEEDED);
    }

    /** 加载课程 */
    private void loadCourses() {
        try {
            accordion.getPanes().clear();
            if (mode == CourseViewMode.ADD) {
                scrollPane.setContent(createAddCoursePane());
                return;}
            if (mode == CourseViewMode.DELETE) {
                loadDeleteCourses();
                return;}
            List<Course> courses = (mode == CourseViewMode.QUERY && queryKeyword != null)
                    ? controller.queryCourses(queryKeyword)
                    : controller.viewAllCourses();

            if (courses == null || courses.isEmpty()) {
                Label emptyLabel = new Label("没有找到课程");
                TitledPane pane = new TitledPane("提示", emptyLabel);
                pane.getStyleClass().add("custom-titled-pane");
                accordion.getPanes().add(pane);
                return;
            }

            for (Course course : courses) {
                TitledPane pane = new TitledPane();
                pane.getStyleClass().add("custom-titled-pane");
                pane.setCollapsible(true);
                pane.setExpanded(false);

                // ===== 课程行 HBox =====
                HBox courseRow = new HBox();
                courseRow.getStyleClass().add("course-row");
                courseRow.setAlignment(Pos.CENTER_LEFT);
                courseRow.setSpacing(5);
                courseRow.setMaxWidth(Double.MAX_VALUE);

                VBox courseIdCol = new VBox(2);
                courseIdCol.setPrefWidth(140);
                Label lblId = new Label(" " + course.getCourseId());
                lblId.setAlignment(Pos.CENTER_LEFT);
                Label detailLink = new Label("课程详情");
                detailLink.getStyleClass().add("detail-link");
                detailLink.setPadding(new Insets(0, 0, 0, 8));
                detailLink.setOnMouseClicked(e -> pane.setExpanded(!pane.isExpanded()));
                courseIdCol.getChildren().addAll(lblId, detailLink);

                Label lblName = new Label(course.getCourseName());
                lblName.setMaxWidth(Double.MAX_VALUE);
                lblName.setAlignment(Pos.CENTER_LEFT);
                HBox.setHgrow(lblName, Priority.ALWAYS);

                Label lblCredit = new Label(String.valueOf(course.getCredit()));
                lblCredit.setPrefWidth(80);
                lblCredit.setAlignment(Pos.CENTER);
                // 列：开课学院
                Label lblDepartmentValue = new Label(course.getDepartment());
                lblDepartmentValue.setPrefWidth(120);
                lblDepartmentValue.setAlignment(Pos.CENTER);

                Label lblClassCount = new Label(String.valueOf(course.getClassrooms().size()));
                lblClassCount.setPrefWidth(100);
                lblClassCount.setAlignment(Pos.CENTER_RIGHT);

                courseRow.getChildren().addAll(courseIdCol, lblName, lblCredit,lblDepartmentValue ,lblClassCount);
                courseRow.prefWidthProperty().bind(scrollPane.widthProperty().subtract(10));
                courseRow.maxWidthProperty().bind(scrollPane.widthProperty().subtract(10));
                pane.setGraphic(courseRow);

                // ===== 班级容器 + 工具栏 =====
                VBox contentBox = new VBox(10);
                contentBox.setPadding(new Insets(5, 10, 10, 10));

                HBox toolbar = new HBox(10);
                toolbar.getStyleClass().add("toolbar");

                Button btnAddClass = new Button("添加班级");
                btnAddClass.getStyleClass().add("toolbar-button");

                Button btnUpdateCourse = new Button("修改课程信息");
                btnUpdateCourse.getStyleClass().add("toolbar-button");

                Button btnDeleteMode = new Button("删除班级");
                btnDeleteMode.getStyleClass().add("toolbar-button");

                toolbar.getChildren().addAll(btnAddClass, btnUpdateCourse, btnDeleteMode);

                FlowPane classContainer = new FlowPane();
                classContainer.setHgap(10);
                classContainer.setVgap(10);
                classContainer.setPadding(new Insets(10, 0, 0, 0));

                // 初始渲染
                renderClassrooms(course, classContainer, false);

                // 按钮绑定事件
                btnAddClass.setOnAction(e -> {
                    try {
                        Classroom newClassroom = openAddClassDialog(course);
                        if (newClassroom != null) {
                            String result = controller.addClassroom(newClassroom);
                            showAlert(result);
                            loadCourses();
                        }
                    } catch (Exception ex) {
                        ex.printStackTrace();
                        showAlert("添加班级失败: " + ex.getMessage());
                    }
                });

                btnUpdateCourse.setOnAction(e -> {
                    try {
                        Course updated = openUpdateCourseDialog(course);
                        if (updated != null) {
                            String result = controller.updateCourseInfo(
                                    updated.getCourseId(), updated.getCourseName(), updated.getCredit()
                            , updated.getDepartment());
                            showAlert(result);
                            loadCourses();
                        }
                    } catch (Exception ex) {
                        ex.printStackTrace();
                        showAlert("修改课程失败: " + ex.getMessage());
                    }
                });

                final boolean[] deleteMode = {false};
                btnDeleteMode.setOnAction(e -> {
                    deleteMode[0] = !deleteMode[0];
                    classContainer.getChildren().clear();
                    renderClassrooms(course, classContainer, deleteMode[0]);
                });

                contentBox.getChildren().addAll(toolbar, classContainer);
                pane.setContent(contentBox);

                accordion.getPanes().add(pane);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private void loadDeleteCourses() {
        try {
            List<Course> courses = controller.viewAllCourses();
            VBox deleteBox = new VBox(10);
            deleteBox.setPadding(new Insets(20));

            if (courses == null || courses.isEmpty()) {
                deleteBox.getChildren().add(new Label("没有课程可以删除"));
            } else {
                for (Course course : courses) {
                    HBox row = new HBox(10);
                    row.setAlignment(Pos.CENTER_LEFT);

                    Label lbl = new Label(course.getCourseId() + " - " + course.getCourseName());
                    lbl.setMaxWidth(Double.MAX_VALUE);
                    HBox.setHgrow(lbl, Priority.ALWAYS);

                    Button btnDelete = new Button("❌ 删除");
                    btnDelete.setOnAction(e -> {
                        Alert confirm = new Alert(Alert.AlertType.CONFIRMATION,
                                "确认要删除课程 " + course.getCourseId() + " 吗？",
                                ButtonType.YES, ButtonType.NO);
                        confirm.showAndWait().ifPresent(result -> {
                            if (result == ButtonType.YES) {
                                try {
                                    String msg = controller.deleteCourse(course.getCourseId());
                                    showAlert(msg);
                                    loadCourses(); // 刷新列表
                                } catch (Exception ex) {
                                    ex.printStackTrace();
                                    showAlert("删除失败: " + ex.getMessage());
                                }
                            }
                        });
                    });

                    row.getChildren().addAll(lbl, btnDelete);
                    deleteBox.getChildren().add(row);
                }
            }

            scrollPane.setContent(deleteBox);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /** 渲染教学班卡片 */
    private void renderClassrooms(Course course, FlowPane container, boolean deleteMode) {
        for (Classroom c : course.getClassrooms()) {
            container.getChildren().add(createClassCard(c, deleteMode));
        }
    }

    /** 班级卡片 */
    private VBox createClassCard(Classroom c, boolean deleteMode) {
        VBox contentBox = new VBox(8); // 存放班级信息 & 按钮
        contentBox.setPadding(new Insets(10));

        Label lblId = new Label("班级编号: " + c.getClassId());
        Label lblTeacher = new Label("教师: " + (c.getTeacher() != null ? c.getTeacher().getName() : ""));
        Label lblTime = new Label("上课时间: " + c.getCourseTime());
        Label lblLoc = new Label("上课地点: " + c.getCourseLocation());
        Label lblCap = new Label("人数/容量: " + c.getSelectedCount() + "/" + c.getCapacity());
        Label lblType = new Label("班级性质: " + (c.getClassType() != null ? c.getClassType() : "未设置"));

        HBox btnBox = new HBox(5);

        if (deleteMode) {
            Button btnDelete = new Button("❌");
            btnDelete.getStyleClass().add("delete-icon-button");
            btnDelete.setOnAction(e -> {
                try {
                    String result = controller.deleteClassroom(c.getClassId());
                    showAlert(result);
                    loadCourses();
                } catch (Exception ex) {
                    ex.printStackTrace();
                    showAlert("删除失败: " + ex.getMessage());
                }
            });

            // 把 ❌ 固定在右上角
            StackPane wrapper = new StackPane(contentBox, btnDelete);
            StackPane.setAlignment(btnDelete, Pos.TOP_RIGHT);
            wrapper.getStyleClass().add("card");

            contentBox.getChildren().addAll(lblId, lblTeacher, lblTime, lblLoc, lblCap, lblType);
            return new VBox(wrapper);

        } else {
            Button btnUpdate = new Button("修改教学班信息");
            btnUpdate.getStyleClass().add("transparent-button");
            btnUpdate.setOnAction(e -> {
                try {
                    Classroom updated = openUpdateClassDialog(c);
                    if (updated != null) {
                        String result = controller.updateClassroom(updated);
                        showAlert(result);
                        loadCourses();
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                    showAlert("修改失败: " + ex.getMessage());
                }
            });

            Button btnStudents = new Button("修改学生名单");
            btnStudents.getStyleClass().add("transparent-button");
            btnStudents.setOnAction(e -> openStudentTable(c.getClassId()));

            btnBox.getChildren().addAll(btnUpdate, btnStudents);

            contentBox.getChildren().addAll(lblId, lblTeacher, lblTime, lblLoc, lblCap, lblType, btnBox);

            VBox card = new VBox(contentBox);
            card.getStyleClass().add("card");
            return card;
        }
    }


    /// ================= 添加班级 =================
    private Classroom openAddClassDialog(Course course) {
        Stage dialog = new Stage();
        dialog.setTitle("添加班级");

        TextField txtClassId = new TextField();
        txtClassId.setPromptText("请输入班级ID");

        // 教师下拉框
        ComboBox<Teacher> teacherComboBox = new ComboBox<>();
        try {
            List<Teacher> teachers = controller.viewAllTeachers();
            teacherComboBox.getItems().addAll(teachers);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        teacherComboBox.setPromptText("请选择任课教师");
        teacherComboBox.setCellFactory(cb -> new ListCell<>() {
            @Override
            protected void updateItem(Teacher teacher, boolean empty) {
                super.updateItem(teacher, empty);
                setText(empty || teacher == null ? null : teacher.getTeacherId() + " - " + teacher.getName());
            }
        });
        teacherComboBox.setButtonCell(new ListCell<>() {
            @Override
            protected void updateItem(Teacher teacher, boolean empty) {
                super.updateItem(teacher, empty);
                setText(empty || teacher == null ? null : teacher.getTeacherId() + " - " + teacher.getName());
            }
        });

        TextField txtTime = new TextField();
        txtTime.setPromptText("请输入上课时间");

        TextField txtLocation = new TextField();
        txtLocation.setPromptText("请输入上课地点");

        TextField txtCapacity = new TextField();
        txtCapacity.setPromptText("请输入容量");

        ComboBox<String> classTypeComboBox = new ComboBox<>();
        classTypeComboBox.getItems().addAll("首修", "重修");
        classTypeComboBox.setPromptText("请选择班级性质");

        final Classroom[] result = {null};

        Button btnSave = new Button("保存");
        btnSave.setOnAction(ev -> {
            try {
                Classroom newClassroom = new Classroom();
                newClassroom.setClassId(txtClassId.getText());
                newClassroom.setCourseId(course.getCourseId());

                Teacher selectedTeacher = teacherComboBox.getValue();
                if (selectedTeacher == null) {
                    showAlert("请选择任课教师！");
                    return;
                }
                newClassroom.setTeacher(selectedTeacher);

                newClassroom.setCourseTime(txtTime.getText());
                newClassroom.setCourseLocation(txtLocation.getText());
                newClassroom.setCapacity(Integer.parseInt(txtCapacity.getText()));
                newClassroom.setSelectedCount(0);

                String type = classTypeComboBox.getValue();
                if (type == null) {
                    showAlert("请选择班级性质！");
                    return;
                }
                newClassroom.setClassType(type);

                result[0] = newClassroom;
                dialog.close();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        });

        VBox root = new VBox(10,
                new Label("班级ID:"), txtClassId,
                new Label("任课教师:"), teacherComboBox,
                new Label("上课时间:"), txtTime,
                new Label("上课地点:"), txtLocation,
                new Label("容量:"), txtCapacity,
                new Label("班级性质:"), classTypeComboBox,
                btnSave
        );
        root.setPrefSize(350, 450);
        dialog.setScene(new Scene(root));
        dialog.showAndWait();
        return result[0];
    }

    // ================= 修改课程 =================
    // ================= 修改课程 =================
    private Course openUpdateCourseDialog(Course course) {
        Stage dialog = new Stage();
        dialog.setTitle("修改课程信息 - " + course.getCourseId());

        Label lblId = new Label(course.getCourseId());
        TextField txtName = new TextField(course.getCourseName());
        TextField txtCredit = new TextField(String.valueOf(course.getCredit()));
        TextField txtDepartment = new TextField(course.getDepartment());  // 开课学院
        txtDepartment.setPromptText("请输入开课学院");

        final Course[] result = {null};

        Button btnSave = new Button("保存");
        btnSave.setOnAction(e -> {
            try {
                Course updated = new Course();
                updated.setCourseId(course.getCourseId());
                updated.setCourseName(txtName.getText());
                updated.setCredit(Integer.parseInt(txtCredit.getText()));
                updated.setDepartment(txtDepartment.getText());
                result[0] = updated;
                dialog.close();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        });

        VBox root = new VBox(10,
                new Label("课程编号:"), lblId,
                new Label("课程名称:"), txtName,
                new Label("学分:"), txtCredit,
                new Label("开课学院:"), txtDepartment,
                btnSave
        );
        root.setPrefSize(350, 250);
        dialog.setScene(new Scene(root));
        dialog.showAndWait();
        return result[0];
    }


    // ================= 修改教学班 =================
    private Classroom openUpdateClassDialog(Classroom classroom) {
        Stage dialog = new Stage();
        dialog.setTitle("修改教学班 - " + classroom.getClassId());

        // 教师下拉框
        ComboBox<Teacher> teacherComboBox = new ComboBox<>();
        try {
            List<Teacher> teachers = controller.viewAllTeachers();
            teacherComboBox.getItems().addAll(teachers);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        teacherComboBox.setValue(classroom.getTeacher());
        teacherComboBox.setCellFactory(cb -> new ListCell<>() {
            @Override
            protected void updateItem(Teacher teacher, boolean empty) {
                super.updateItem(teacher, empty);
                setText(empty || teacher == null ? null : teacher.getTeacherId() + " - " + teacher.getName());
            }
        });
        teacherComboBox.setButtonCell(new ListCell<>() {
            @Override
            protected void updateItem(Teacher teacher, boolean empty) {
                super.updateItem(teacher, empty);
                setText(empty || teacher == null ? null : teacher.getTeacherId() + " - " + teacher.getName());
            }
        });

        TextField txtTime = new TextField(classroom.getCourseTime());
        TextField txtLocation = new TextField(classroom.getCourseLocation());
        TextField txtCapacity = new TextField(String.valueOf(classroom.getCapacity()));

        ComboBox<String> classTypeComboBox = new ComboBox<>();
        classTypeComboBox.getItems().addAll("首修", "重修");
        classTypeComboBox.setValue(classroom.getClassType());

        final Classroom[] result = {null};

        Button btnSave = new Button("保存");
        btnSave.setOnAction(e -> {
            try {
                Classroom updated = new Classroom();
                updated.setClassId(classroom.getClassId());
                updated.setCourseId(classroom.getCourseId());
                updated.setTeacher(teacherComboBox.getValue());
                updated.setCourseTime(txtTime.getText());
                updated.setCourseLocation(txtLocation.getText());
                updated.setCapacity(Integer.parseInt(txtCapacity.getText()));
                updated.setSelectedCount(classroom.getSelectedCount());
                updated.setClassType(classTypeComboBox.getValue());
                result[0] = updated;
                dialog.close();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        });

        VBox root = new VBox(10,
                new Label("任课教师:"), teacherComboBox,
                new Label("上课时间:"), txtTime,
                new Label("上课地点:"), txtLocation,
                new Label("容量:"), txtCapacity,
                new Label("班级性质:"), classTypeComboBox,
                btnSave
        );
        root.setPrefSize(350, 400);
        dialog.setScene(new Scene(root));
        dialog.showAndWait();
        return result[0];
    }


    private void openStudentTable(String classId) {
        Stage stage = new Stage();
        stage.setTitle("修改学生名单 - 班级 " + classId);

        TableView<Student> table = new TableView<>();
        ObservableList<Student> students = FXCollections.observableArrayList();

        try {
            List<Student> existingStudents = controller.viewStudentsInClass(classId);
            if (existingStudents != null) students.addAll(existingStudents);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        table.setItems(students);

        TableColumn<Student, String> colId = new TableColumn<>("学号");
        colId.setCellValueFactory(data -> new javafx.beans.property.SimpleStringProperty(data.getValue().getStudentId()));
        colId.setPrefWidth(150);

        TableColumn<Student, String> colName = new TableColumn<>("姓名");
        colName.setCellValueFactory(data -> new javafx.beans.property.SimpleStringProperty(data.getValue().getStudentName()));
        colName.setPrefWidth(150);

        TableColumn<Student, Void> colAction = new TableColumn<>("操作");
        colAction.setCellFactory(param -> new TableCell<>() {
            private final Button btnRemove = new Button("移除");

            {
                btnRemove.setOnAction(e -> {
                    Student s = getTableView().getItems().get(getIndex());
                    Alert confirm = new Alert(Alert.AlertType.CONFIRMATION,
                            "确认移除学生 " + s.getStudentId() + " 吗？", ButtonType.YES, ButtonType.NO);
                    confirm.showAndWait().ifPresent(result -> {
                        if (result == ButtonType.YES) {
                            try {
                                String msg = controller.removeStudentFromClass(classId, s.getStudentId());
                                showAlert(msg);
                                table.getItems().remove(s);
                                loadCourses();
                            } catch (Exception ex) {
                                ex.printStackTrace();
                            }
                        }
                    });
                });
            }

            @Override
            protected void updateItem(Void item, boolean empty) {
                super.updateItem(item, empty);
                setGraphic(empty ? null : btnRemove);
            }
        });

        table.getColumns().addAll(colId, colName, colAction);

        TextField txtStudentId = new TextField();
        txtStudentId.setPromptText("输入学生ID");
        Button btnAdd = new Button("添加学生");
        btnAdd.setOnAction(e -> {
            String newId = txtStudentId.getText().trim();
            if (!newId.isEmpty()) {
                try {
                    String msg = controller.addStudentToClass(classId, newId);
                    showAlert(msg);
                    students.clear();
                    students.addAll(controller.viewStudentsInClass(classId));
                    txtStudentId.clear();
                    loadCourses();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });

        HBox addBox = new HBox(5, txtStudentId, btnAdd);
        VBox root = new VBox(10, table, addBox);
        root.setPrefSize(500, 400);
        stage.setScene(new Scene(root));
        stage.show();
    }
    private VBox createAddCoursePane() {
        VBox box = new VBox(12);
        box.setPadding(new Insets(20));

        TextField txtId = new TextField();
        txtId.setPromptText("课程编号");

        TextField txtName = new TextField();
        txtName.setPromptText("课程名称");

        TextField txtCredit = new TextField();
        txtCredit.setPromptText("学分");

        TextField txtDepartment = new TextField();
        txtDepartment.setPromptText("开课学院");


        Button btnSave = new Button("保存");
        btnSave.getStyleClass().add("primary-button");
        btnSave.setOnAction(e -> {
            try {
                String id = txtId.getText().trim();
                String name = txtName.getText().trim();
                int credit = Integer.parseInt(txtCredit.getText().trim());
                String department = txtDepartment.getText().trim();

                String result = controller.addCourse(id, name, credit, department);
                showAlert(result);

                // 保存成功 → 切回 ALL 模式
                AdminCourseView cv = new AdminCourseView(CourseViewMode.ALL);
                BorderPane parent = (BorderPane) scrollPane.getParent();
                parent.setCenter(cv.getView());

            } catch (Exception ex) {
                ex.printStackTrace();
                showAlert("添加失败: " + ex.getMessage());
            }
        });

        Button btnCancel = new Button("返回");
        btnCancel.setOnAction(e -> {
            try {
                AdminCourseView cv = new AdminCourseView(CourseViewMode.ALL);
                BorderPane parent = (BorderPane) scrollPane.getParent();
                parent.setCenter(cv.getView());
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        });

        box.getChildren().addAll(
                new Label("课程编号:"), txtId,
                new Label("课程名称:"), txtName,
                new Label("学分:"), txtCredit,
                new Label("开课学院:"), txtDepartment,
                new HBox(10, btnSave, btnCancel)
        );

        return box;
    }



    private void showAlert(String msg) {
        Alert alert = new Alert(Alert.AlertType.INFORMATION, msg, ButtonType.OK);
        alert.showAndWait();
    }
}
